#! /usr/bin/env python
# coding=utf-8
import os, json, urllib, urllib2
from custservice.models import *
from common.urlUtil import *
from common.dateUtil import *
import datetime,settings

#搜索引擎组件,提供以下这些方法
#searchAppraisal:搜索评价内容
#searchChatAndQues:搜索聊天内容以及常见问题,完成
#writeChatContent:写入聊天内容,写入Lucene索引
#writeAppraisal:写入评价
#writeFreqQues:写入常见问题
#initIndex:初始化索引，这个方法要小心使用，一旦使用，其会清除索引目录下的内容。
class SearchEngine:
    #搜索评价内容
    def searchAppraisal(self, param):
        requestDict = {
            "method": "searchAppraisal"
            }
        requestDict.update(param)

        return self._callJava(requestDict)

    #搜索聊天内容以及常见问题
    def searchChatAndQues(self, param):
        requestDict = {
            "method": "searchChatAndQues"
            }
        requestDict.update(param)

        return self._callJava(requestDict)

    #写入聊天内容,写入Lucene索引
    def writeChatContent(self, param):
        requestDict = {
            "method": "writeChatContent"
            }
        requestDict.update(param)

        return self._callJava(requestDict)

    #写入常见问题
    def writeFreqQues(self, param):
        requestDict = {
            "method": "writeFreqQues"
            }
        requestDict.update(param)

        return self._callJava(requestDict)

    #写入评价
    def writeAppraisal(self, param):
        requestDict = {
            "method": "writeAppraisal"
            }
        requestDict.update(param)

        return self._callJava(requestDict)

    #初始化索引，这个方法要小心使用，一旦使用，其会清除索引目录下的内容。
    def initIndex(self):
        requestDict = {
            "method": "initIndex"
            }

        return self._callJava(requestDict)

    def _callJava(self, requestDict):
        param = {
            'q': json.dumps(requestDict)
            }
        data = urllib.urlencode(param)
        req = urllib2.Request(settings.JAVA_SEARCH_PATH, data)
        fIn = urllib2.urlopen(req)
        content = fIn.read()
        fIn.close()

        return json.loads(content)

def test2():
    # init index
    searchEngine = SearchEngine()
    print searchEngine.initIndex()

    # write chat content
    requestDict = {
        "method": "writeChatContent",
        "id": 2,
        "deptId": 2,
        "title": u"测试\n\"asdflaksdjf",
        "content": u"测试\n\"asldfjasdf",
        "userId": 1,
        "createTime":"2011-02-05 22:45:33"
        }
    print searchEngine.writeChatContent(requestDict)

    # search chat and ques
    requestDict = {
        "method": "searchChatAndQues",
        "deptId": 2,
        "content": u"测试",
        "pageNo": 1,
        "pageSize": 10
        }
    print searchEngine.searchChatAndQues(requestDict)

def test1():
    searchEngine = SearchEngine()
    searchEngine.initIndex()

    deptId = 2
    content = 'test'
    pageNo = 1
    pageSize = 10
    searchChatAndQuesDict = {
        'deptId': deptId,
        'content': content,
        'pageNo': pageNo,
        'pageSize': pageSize
        }

    print searchEngine.searchChatAndQues(searchChatAndQuesDict)

    chatRecordLi = ChatRecord.objects.all()

    for item in chatRecordLi:
        print item.content
        requestDict = {
            'id': int(item.id),
            'deptId': int(item.dept_id),
            'title': item.content,
            'content': item.content,
            'userId': int(item.user_id),
            'createTime': getDateTimeYMDHMS(item.createTime)
            }

        print searchEngine.writeChatContent(requestDict)

    customerFAQLi = CustomerFAQ.objects.all()
    for item in customerFAQLi:
        requestDict = {
            'id': int(item.id),
            'deptId': int(item.dept_id),
            'title': item.question,
            'content': item.answer,
            'userId': int(item.user_id),
            'createTime': getDateTimeYMDHMS(item.createTime)
            }
        print searchEngine.writeFreqQues(requestDict)

#这个方法可以做为写入参照,在间隔之间进行参照
def writeChatTest():
    from django.db import connection
    sql = '''
select distinct(user_id) 
from custservice_chatrecord
'''
    cursor = connection.cursor()
    cursor.execute(sql)
    distinctUserIdLi = cursor.fetchall()
    distinctUserIdLi = [item[0] for item in distinctUserIdLi]
    for userId in distinctUserIdLi:
        userChatLi = ChatRecord.objects.filter(user_id=userId)
        if len(userChatLi) > 1:
            for i in range(0, len(userChatLi) - 1):
                userChatBefore = userChatLi[i]
                userChatAfter = userChatLi[i + 1]
#                beginCreateTime = 
            pass
        else:
            pass
    

